package com.dingding.activity.core.activity.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.activity.core.activity.dao.ICouponCacheDao;
import com.dingding.activity.core.activity.dao.ICouponDao;
import com.dingding.activity.core.activity.dao.sql.CouponSQL;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.beans.activity._Coupon;
import com.dingding.common.core.cache.CacheConstants;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.common.core.util.JSONUtil;

/**
 * 
 * @Description: 优惠券DAO
 * @author CHEHUANBO
 * @date 2016年12月9日
 * @version V2.6.2
 * 
 */
@Component("_couponDao")
public class CouponDaoImpl implements ICouponDao, ICouponCacheDao {

	@Resource(name = "SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	@Resource(name = "_couponDao")
	private ICouponDao couponDao;

	private static final Logger log = Logger.getLogger(CouponDaoImpl.class);

	/**
	 * 
	 * 是否初始化数据 说明：由于编写此功能的时候没有后台活动管理系统，为保证数据统一。在项目重启后再次调用时需要同步数据到缓存中
	 * 
	 */
	private static boolean is_intital = true;

	/**
	 * 
	 * @Description: 获取当前活动关联的优惠券
	 * @param couponId
	 *            优惠券ID
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public List<_Coupon> getCouponByActivityIdToDb(String activityId) {

		if (StringUtils.isBlank(activityId)) {
			throw new NullPointerException("通过活动ID从缓存中获取关联优惠券出现异常！原因：activityId is null");
		}

		try {

			List<_Coupon> couponList = sqlBaseDao.listSqlToCustomBean(_Coupon.class, CouponSQL.GET_COUPON_BY_ACTIVITYID,
					activityId);

			return couponList;

		} catch (Exception e) {
			log.error("通过活动ID从DB中获取关联优惠券出现异常！", e);
		}

		return null;

	}

	/**
	 * 
	 * @Description: 通过活动ID从缓存中获取关联优惠券
	 * @param activityId
	 *            活动ID
	 * @return List<_Coupon> 优惠券集合
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public List<_Coupon> getCouponByActivityIdToCache(String activityId) {

		try {

			String cacheKey = CacheConstants.ACTIVITY_COUPON_LIST + activityId;

			List<_Coupon> couponList = null;
			
			if (is_intital) {
				couponList = couponDao.getCouponByActivityIdToDb(activityId);
				JedisUtil.setValue(CacheConstants.ACTIVITY_COUPON_LIST + activityId, JSONUtil.toJSONString(couponList),
						DbEnum.ActivityDb.getValue());
			} else {
				String cacheValue = JedisUtil.getValue(cacheKey, DbEnum.ActivityDb.getValue());
				return JSONUtil.parseArray(cacheValue, _Coupon.class);
			}

			return couponList;

		} catch (Exception e) {
			log.error("通过活动ID从缓存中获取关联优惠券出现异常！活动ID：" + activityId, e);
		}

		return null;

	}

}
